﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="EmailSetting.aspx.cs" Inherits="CRMWeb.Pages.Admin.EmailSetting" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Email Account Setting</title>
</head>
<body>
    <table id="dgEmailAcc" title="Email Account Setting" style="width: 600%; height: 380px; overflow-y: auto;"
        toolbar="#toolbarSetting" pagination="true" data-options="pageSize:10,pageList: [10,20,30,50]" rownumbers="true" fitcolumns="true" singleselect="true" autorowheight="true">
        <thead>
            <tr>
                <th field="Company" width="200" align="center">Company</th>
                <th field="EmailAccount" width="500px" align="center">EmailAccount</th>
                <th field="IsDefault" width="80" formatter="formatCheckbox" align="center">Default</th>
            </tr>
        </thead>
    </table>
    <div id="toolbarSetting">
        <a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-add" plain="true" onclick="newSetting()">New</a>
        <a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-edit" plain="true" onclick="editSetting()">Edit</a>
        <a id="lnkDelete" href="javascript:void(0)" class="easyui-linkbutton" onclick="openDelESPopup()" plain="true" iconcls="icon-remove" title="Delete Email Account Setting">Delete</a>
    </div>
    <div id="dlgSetting" class="easyui-dialog" style="width: 420px; height: 180px; padding: 10px"
        closed="true" buttons="#dlg-buttons">
        <form id="fmdlgSetting" method="post" runat="server" novalidate>
            <div class="fitem" style="margin-bottom: 10px">
                <label style="width: 97px;">Company:</label>
                <input class="easyui-combobox" id="cmbEmailCompany" style="width: 267px;" required="required"
                    data-options="panelWidth:265,  
                    editable:false,
                        valueField: 'MemberGroupName',  
                        textField: 'MemberGroupName',
                        url: '/WebServices/Notice.ashx?method=BindAllRole',
                    selected:true" />
            </div>
            <div class="fitem" style="margin-bottom: 10px">
                <label style="width: 97px;">Email Account:</label>
                <input class="easyui-combobox" id="cmbEmailAcc" style="width: 267px;" required="required" data-options="panelWidth:265,  
                    editable:false,
                        valueField: 'EmailId',  
                        textField: 'EmailId',
                        url: '/WebServices/EmailAccountSetting.ashx?method=BindEmailAccounts',
                    selected:true" />
            </div>
            <div class="fitem" style="margin-bottom: 10px">
                <label style="width: 97px;">Default Account:</label>
                <input type="checkbox" id="is_active" name="is_active" value="true" />
            </div>
            <input id="hdnID" value="" style="display: none" />
        </form>
    </div>
    <div id="dlg-buttons">
        <a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-cancel" onclick="javascript:$('#dlgSetting').dialog('close')" style="float: left">Cancel</a>
        <a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-ok" onclick="saveSetting()">Save</a>
    </div>

    <script type="text/javascript">


        function newSetting() {
            $("#cmbEmailCompany").combobox('reload');
            $('#cmbEmailCompany').combobox('textbox').bind('click', function () {
                $('#cmbEmailCompany').combobox('showPanel');
            });
            $('#cmbEmailAcc').combobox('textbox').bind('click', function () {
                $('#cmbEmailAcc').combobox('showPanel');
            });
            $('#cmbEmailCompany').combobox({
                onLoadSuccess: function (data) {
                    $(this).combobox('select', data[0]['MemberGroupName']);
                },
            });
            $('#hdnNoticeID').val('');
            $('#dlgSetting').dialog('open').dialog('setTitle', 'New Email Account Setting');
            $('#fmdlgSetting').form('clear');
            url = '/WebServices/EmailSettingHandler/AddSettings';
        }

        //Resize datagrid:
        window.onresize = function () {
            $('#dgEmailAcc').datagrid('resize', {
                width: window.innerWidth - 165,
                height: window.innerHeight - 40
            });
        }
        function editSetting() {
            var row = $('#dgEmailAcc').datagrid('getSelected');
            $('#cmbEmailCompany').combobox('textbox').bind('click', function () {
                $('#cmbEmailCompany').combobox('showPanel');
            });

            $('#cmbEmailAcc').combobox('textbox').bind('click', function () {
                $('#cmbEmailAcc').combobox('showPanel');
            });
            if (row) {

                $('#dlgSetting').dialog('open').dialog('setTitle', 'Edit Email Account Setting');
                $('#fmdlgSetting').form('load', row);


                $('#hdnID').val(row.ID);
                $('#cmbEmailCompany').combobox('setValue', row.Company);
                $('#cmbEmailAcc').combobox('setValue', row.EmailAccount);

                if (row.IsDefault == "1") {
                    $('#is_active').prop('checked', true);
                }
                else {
                    $('#is_active').prop('checked', false);
                }
                url = '/WebServices/EmailAccountSetting/EditSettings?ID=' + row.ID;
            }
        }

        function openDelESPopup() {
            var row = $('#dgEmailAcc').datagrid('getSelected');
            if (row) {
                $('#headertext').html("Are you sure want to delete this Email setting ?");
                $('#dlgDelconfirm').dialog('open');
                $('#btnDelete').bind("click", function () {
                    deleteEmailSetting();
                });
            }
        }


        function deleteEmailSetting() {
            var row = $('#dgEmailAcc').datagrid('getSelected');
            if (row) {
                $.ajax({
                    url: '/WebServices/EmailAccountSetting.ashx',
                    type: 'POST',
                    data: {
                        method: 'DeleteSettings',
                        args: { ID: row.ID }
                    },
                    success: function (data) {
                        $('#dgEmailAcc').datagrid('reload');
                        $('#dlgDelconfirm').dialog('close');
                    }
                });
            }
        }


        $(document).ready(function () {
            $('#dgEmailAcc').datagrid({
                url: '/WebServices/EmailAccountSetting.ashx?method=GetEmailAccountData',
                onDblClickRow: function (index, rowdata) {
                    editSetting();
                }
            });
        });

        function formatCheckbox(value, row, index) {

            if (value == 1) {
                return "<input type='checkbox' checked=true  onclick='return false'>";
            }

        }

        function saveSetting() {

            var Company = $('#cmbEmailCompany').combobox('getValue');
            var EmailAccount = $('#cmbEmailAcc').combobox('getValue');
            var IsDefault = $('#is_active').attr('checked') ? 1 : 0;
            var ID = $('#hdnID').val();
            if (EmailAccount == '') {
                $.messager.alert('Morgan CRM', 'Please select email id...!', 'warning');
            }
            else {

                if (ID != '') {
                    if (Company != '') {

                        $.ajax({
                            url: '/WebServices/EmailAccountSetting.ashx',
                            type: 'POST',
                            data: {
                                method: 'EditSettings',
                                args: { ID: ID, Company: Company, EmailAccount: EmailAccount, IsDefault: IsDefault }
                            },
                            success: function (data) {
                                switch (data) {
                                    case "0":
                                        $.messager.alert('Morgan CRM', 'This account setting already exists.', 'info');
                                        break;
                                    case "1":
                                        $('#dlgSetting').dialog('close');
                                        $('#dgEmailAcc').datagrid('reload');
                                        break;

                                    case "2":
                                        $.messager.alert('Morgan CRM', 'One account only have one default setting.', 'info');
                                        break;
                                }
                            }
                        });
                    }
                }
                else {
                    $.ajax({
                        url: '/WebServices/EmailAccountSetting.ashx',
                        type: 'POST',
                        data: {
                            method: 'AddSettings',
                            args:
                                { Company: Company, EmailAccount: EmailAccount, IsDefault: IsDefault }
                        },
                        success: function (data) {
                            switch (data) {
                                case "0":
                                    $.messager.alert('Morgan CRM', 'This account setting already exists.', 'info');
                                    break;

                                case "1":
                                    $('#dlgSetting').dialog('close');
                                    $('#dgEmailAcc').datagrid('reload');
                                    break;

                                case "2":
                                    $.messager.alert('Morgan CRM', 'One account only have one default setting.', 'info');
                                    break;
                            }
                        }
                    });
                }
            }
        }

    </script>
</body>
</html>
